package com.cristina.attention.mp.base;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cristina.attention.base.BaseEntity;
import com.cristina.attention.utils.SecurityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotEmpty;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author by superman
 * @Date 2021/12/9
 */
@Validated
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {

    @Override
    public boolean save(T entity) {
        String currentUsername = SecurityUtils.getCurrentUsername();
        entity.setCreateBy(currentUsername);
        entity.setUpdateBy(currentUsername);

        long currentTimeMillis = System.currentTimeMillis();
        entity.setCreateTime(currentTimeMillis);
        entity.setUpdateTime(currentTimeMillis);

        return super.save(entity);
    }

    @Override
    public boolean updateById(T entity) {
        String currentUsername = SecurityUtils.getCurrentUsername();
        entity.setUpdateBy(currentUsername);
        entity.setUpdateTime(System.currentTimeMillis());
        return super.updateById(entity);
    }

    @Override
    public boolean deleteLogic(@NotEmpty List<Long> ids) {
        return super.removeByIds(ids);
    }
}
